<%--
  @author Kusal Samarakoon
  @author Supun Viraj Rathnayaka
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery.jqplot.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jquery.jqplot.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.pieRenderer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.barRenderer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/lib/jqplot/jqplot.categoryAxisRenderer.js"></script>
<script src="${pageContext.request.contextPath}/js/printer.js" type="text/javascript"></script>

<script type="text/javascript">

    $(function () {
        $('.land-table').html(initTableHead());
        $('#chartdiv').hide();

        $('select#railwayTrackId').bind('change', function (evt1) {
            var id = $("select#railwayTrackId").attr("value");
            if(id>0){
                $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 6},
                        function (data) {
                            var options1 = '<option value="0">' + $('#all').val() + '</option> ';
                            var ds = data.districtList;
                            for (var i = 0; i < ds.length; i++) {
                                options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'
                            }
                            $("select#districtId").html(options1);
                        });
            }else{
                $("select#districtId").html('<option value="0">' + $('#all').val() + '</option> ');
            }
        });
    });


    function generateStatistics() {
        var crit = [];

        var from = $('input#startDate').val().split("-");
        var to = $('input#endDate').val().split("-");
        //var migId = $('select#migrationState').attr("value");

        crit.push($('select#branchId').attr("value"));
        crit.push($('select#railwayTrackId').attr("value"));
        crit.push($('select#districtId').attr("value"));
        crit.push($('select#landState').attr("value"));
        crit.push(new Date(from[0], (from[1] - 1), from[2]).getTime());
        crit.push(new Date(to[0], (to[1] - 1), to[2]).getTime());

        var type = "LandStat";
        $.getJSON('${pageContext.request.contextPath}/common/RailwayReportService', {type: type, branch: crit[0], track: crit[1], district: crit[2], dtFrom: crit[4], dtTo: crit[5], landSt: crit[3]},function (data) {
            loadLandList(data.landList, crit);
        });
    }

    function loadLandList(landInfo, cr) {
        var opt = '';
        var total = 0;
        opt += initTableHead();
        opt += '<tbody>';
        var list_url = getListingUrl(cr);
        if (landInfo.length > 0) {
            for (var i = 0; i < landInfo.length; i++) {
                opt += '<tr>' +
                        '<td class="center-align">' + getApprovalStateName(landInfo[i][0]) + '</td>' +
                        '<td class="center-align">' + landInfo[i][1] + '</td>' +
                        '<td class="center-align"><a href="'+list_url+'"><img src="${pageContext.request.contextPath}/images/mark-as-complete.png" width="32px" height="32px"/></a></td>' +
                        '</tr>';
                total += landInfo[i][1];
            }
        } else {
            opt += '<tr>' +
                    '<td colspan="7" class="center-align">' +
                    $('#no-record').val() +
                    '</td>' +
                    '</tr>';
        }

        opt += '</tbody>';
        if (landInfo.length > 1) {
            opt += initTableFoot(total);
        }
        $('.land-table').html(opt);
        if (landInfo.length > 0) {
            $('#chartdiv').slideDown("slow", function () {
                showPieChart(landInfo);
            });
        } else {

            $('#chartdiv').slideUp("slow", function () {
                $('#chartdiv').empty();
            });
        }

    }

    function getListingUrl(cr){
        var url = "landDetailsList.do?"
        for(var i in cr) {
            switch (parseInt(i)){
                case 0: // branch id
                    url += "branchId="+cr[i];
                    break;
                case 1: // track  id
                    url += "&railwayTrackId="+cr[i];
                    break;
                case 2: // district id
                    url += "&districtId="+cr[i];
                    break;
                case 3: // land status
                    url += "&landSt="+cr[i];
                    break;
                case 4: // starting date
                    url += "&startingDate="+cr[i];
                    break;
                case 5: // ending date
                    url += "&endingDate="+cr[i];
                    break;
            }
        }
        return url;

    }

    function initTableHead() {
        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#land_status').val() + '</th>' +
                '<th class="center-align">' + $('#no_of_lands').val() + '</th>' +
                '<th class="center-align">Action</th>' +
                '</tr>' +
                '</thead>';
        return opt;
    }

    function initTableFoot(total) {
        var opt = '';
        opt += '<tfoot>' +
                '<tr class="table-header">' +
                '<th class="center-align">' + $('#all_lands_sum').val() + '</th>' +
                '<th class="center-align">' + total + '</th>' +
                '<th class="center-align"></th>' +
                '</tr>' +
                '</tfoot>';
        return opt;
    }

    function showPieChart(landInfo) {

        var data = new Array();

        for (var i = 0; i < landInfo.length; i++) {
            data.push([getApprovalStateName(landInfo[i][0]), landInfo[i][1]]);
        }
        var plot1 = jQuery.jqplot('chartdiv', [data],
                {
                    seriesDefaults: {
                        renderer: jQuery.jqplot.PieRenderer,
                        rendererOptions: {
                            showDataLabels: true
                        }
                    },
                    legend: { show: true, location: 'e', fontSize: 20 }
                }
        );
    }

    function getApprovalStateName(name) {
        var stateName;
        if (name === "REJECTED") {
            stateName = $('#rejected').val();
        } else if (name === "APPROVED") {
            stateName = $('#approved').val();
        } else if (name === "PENDING") {
            stateName = $('#pending').val();
        } else if (name === "DATA_ENTRY") {
            stateName = $('#data_entry').val();
        }
        return stateName;
    }




</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="reportModule.do">
        <span><img src="${pageContext.request.contextPath}/images/print.png" alt="Print">
            <s:label value="%{getText('reports.label')}"/></span>
    </s:a>
    <span class="module-title-breaker"> > </span>
    <s:a href="leaseArrearsReport.do">
        <span><img src="${pageContext.request.contextPath}/images/list.png" alt="">
            <s:label value="Land Statistics Report"/></span>
    </s:a>
    <div id="back-button" align="right">
        <s:form><s:submit value="%{getText('back_btn.label')}" action="reportModule"/></s:form>
    </div>
</div>

<table>
    <tr>
        <td colspan="2" class="left-align">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </td>
    </tr>
</table>

<div id="printbtn" class="form-button-div right-align">
    <input type="button" style="margin:10" id="printBtnId" value="Print" onclick="printPage('init-land-report-list', 'A4', 'P');"/>
</div>

<div id="init-land-report-list" class="module-home">

    <h2><s:label value="%{getText('land_statistics.label')}"/></h2>

    <table class="width-635-px margin-10-auto">
        <tr>
            <td class="left-align"><s:label value="%{getText('branch.label')}"/></td>
            <td class="left-align"><s:label value="%{getText('land_status.label')}"/></td>
        </tr>
        <tr>
            <td class="center-align">
                <s:if test="%{branchList.size==1}">
                    <s:select id="branchId" name="branchId" list="branchList" cssClass="width-250-px"/>
                </s:if>
                <s:else>
                    <s:select id="branchId" name="branchId" list="branchList" headerKey="0"
                              headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
                </s:else>
            </td>
            <td>
                <s:select id="landState" name="landState"
                          list="landStateList" headerKey="0"
                          headerValue="%{getText('all.label')}" cssClass="width-200-px"/>
            </td>
        </tr>

        <tr>
            <td><s:label value="%{getText('railway_line.label')}"/></td>
            <td><s:label value="%{getText('district.label')}"/></td>
        </tr>
        <tr>
            <td class="center-align">
                <s:select id="railwayTrackId" name="trackListId" list="railwayTrackList" headerKey="0"
                          headerValue="%{getText('all.label')}" cssClass="width-250-px"/>
            </td>
            <td class="center-align">
                <select id="districtId" name="districtName" class="width-250-px"><option value="0"><s:property value="%{getText('all.label')}"/></option></select>
                <%--<s:if test="%{districtList.size==1}">--%>
                    <%--<s:select id="districtId" name="districtName" list="districtList" cssClass="width-250-px"/>--%>
                <%--</s:if>--%>
                <%--<s:else>--%>
                    <%--<s:select id="districtId" name="districtName" list="districtList" headerKey="0"--%>
                              <%--headerValue="%{getText('all.label')}" cssClass="width-250-px"/>--%>
                <%--</s:else>--%>
            </td>
        </tr>

        <%--<tr>
            <td><s:label value="%{getText('migration_state.label')}"/></td>
        </tr>

        <tr>

            <td>
                <s:select id="migrationState" name="migrationState"
                          list="migrationStateList" headerKey="0"
                          headerValue="%{getText('all.label')}" cssClass="width-200-px"/>
            </td>
        </tr>--%>
        <tr>
            <td class="left-align">
                <s:label value="%{getText('time_range.label')}"/>&nbsp;<s:label value="%{getText('from.label')}"/>
            </td>

            <td class="left-align">
                <s:label value="%{getText('time_range.label')}"/>&nbsp;<s:label value="%{getText('to.label')}"/>
            </td>
        </tr>
        <tr>
            <td class="left-align">
                <s:textfield cssClass="width-200-px" id="startDate" name="startDate" onclick="selectDate('startDate')"
                             onKeyPress="return dateNumbersOnly(event,true)"/>
            </td>
            <td class="left-align">
                <s:textfield cssClass="width-200-px" id="endDate" name="endDate" onclick="selectDate('endDate')"
                             onKeyPress="return dateNumbersOnly(event,true)"/>
            </td>
        </tr>

        <tr>
            <td colspan="2" class="right-align"><s:submit onClick="generateStatistics()"
                                                          value="%{getText('search.label')}"/></td>
        </tr>
    </table>


    <table class="width-100 margin-10-auto table land-table">
    </table>

    <div id="chartdiv" style="height:500px;width:830px; "></div>
</div>
    <%-- Localization text for data tables --%>
    <s:hidden id="display" value="%{getText('display.label')}"/>
    <s:hidden id="records" value="%{getText('records.label')}"/>
    <s:hidden id="search" value="%{getText('search.label')}"/>
    <s:hidden id="no-record" value="%{getText('no_record.label')}"/>
    <s:hidden id="showing" value="%{getText('showing.label')}"/>
    <s:hidden id="to" value="%{getText('to.label')}"/>
    <s:hidden id="of" value="%{getText('of.label')}"/>
    <s:hidden id="entries" value="%{getText('entries.label')}"/>
    <s:hidden id="first" value="%{getText('first.label')}"/>
    <s:hidden id="last" value="%{getText('last.label')}"/>
    <s:hidden id="next" value="%{getText('next.label')}"/>
    <s:hidden id="previous" value="%{getText('previous.label')}"/>
    <s:hidden id="all" value="%{getText('all.label')}"/>
    <s:hidden id="rejected" value="%{getText('rejected.label')}"/>
    <s:hidden id="approved" value="%{getText('approve.label')}"/>
    <s:hidden id="pending" value="%{getText('pending.label')}"/>
    <s:hidden id="data_entry" value="%{getText('data_entry.label')}"/>
    <s:hidden id="land_status" value="%{getText('land_status.label')}"/>
    <s:hidden id="no_of_lands" value="%{getText('no_of_lands.label')}"/>
    <s:hidden id="all_lands_sum" value="%{getText('all_lands_sum.label')}"/>
